package cn.admin.configcenter.registry.service.client;

import cn.admin.remoting.Connection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientDisconnectJob implements Runnable {

    private static final Logger log = LoggerFactory.getLogger(ClientDisconnectJob.class);

    private final Connection conn;
    private final String originalHostId;
    private final long createTime;
    private final String originalCsIp;

    ClientDisconnectJob(Connection connection) {
        this.conn = connection;
        this.originalHostId = null;
        this.originalCsIp = null;
        this.createTime = System.currentTimeMillis();
    }

    public ClientDisconnectJob(String originalHostId, String originalCsIp) {
        this.originalHostId = originalHostId;
        this.originalCsIp = originalCsIp;
        this.conn = null;
        this.createTime = System.currentTimeMillis();
    }

    @Override
    public void run() {
        long pendingTime = System.currentTimeMillis() - this.createTime;
        if (pendingTime >= 600L) {
            log.warn("[diconnect-client-request] Pending too long in queue. " + pendingTime);
        }

        if (null != this.conn) {
            ClientConnectionWorker.getWorker().removeClient(this.conn);
        } else {
            ClientConnectionWorker.getWorker().removeClusterClient(this.originalHostId,
                    this.originalCsIp);
        }
    }
}
